import { assert, expect } from "chai";
import Promise from "../src/index";

Promise.resolve(0).then(res => {
  console.log(res) // 0
  return Promise.resolve(4)
}).then(res => {
  console.log(res) // 4
})

Promise.resolve(1).then(res => {
  console.log(res) //1
})
.then(() => {
  console.log(2)
})
.then(() => {
  console.log(3)
})
.then(() => {
  console.log(5)
})
.then(() => {
  console.log(6)
})

describe("promise 测试", () => {
  it("then在resolve之前执行", (done) => {
    new Promise((resolve) => resolve(1)).then((res) => {
      assert(res === 1);
      done();
    });
  });
  it("then在reject之前执行", (done) => {
    new Promise((s, j) => j(1)).then(null, (res) => {
      assert(res === 1);
      done();
    });
  });
  it("多次调用 resolve", (done) => {
    let result = 0;
    new Promise((resolve) => {
      resolve(111);
      resolve(222);
      resolve(333);
    }).then((res) => (result = res));
    setTimeout(() => {
      assert(result === 111);
      done();
    });
  });
  it("多次调用 reject", (done) => {
    let result = 0;
    new Promise((resolve, reject) => {
      reject(1);
      reject(2);
      reject(3);
    }).then(null, err => result = err);
    setTimeout(() => {
      assert(result === 1);
      done();
    });
  });
  it("连续调用then之resolve", (done) => {
    let result = "";
    let p = new Promise((resolve) => resolve(1));
    p.then(() => (result += "a"));
    p.then(null, () => (result += "b"));
    p.then(() => (result += "c"));
    setTimeout(() => {
      assert(result === "ac");
      done();
    });
  });
  it("连续调用then之reject", (done) => {
    let result = "";
    let p = new Promise((resolve, reject) => reject(1));
    p.then(() => (result += "a"));
    p.then(null, () => (result += "b"));
    p.then(() => (result += "c"));
    setTimeout(() => {
      assert(result === "b");
      done();
    });
  });
  it("then 链式调用之resolve", (done) => {
    new Promise((s) => s("a"))
      .then((res) => res + "b")
      .then((res) => res + "c")
      .then((res) => {
        assert(res === "abc");
        done();
      });
  });
  it("then 链式调用之reject", (done) => {
    new Promise((s, j) => j("a"))
      .then(null, (res) => res + "b")
      .then((res) => res + "c")
      .then((res) => {
        assert(res === "abc");
        done();
      });
  });
  it("then return Promise对象", (done) => {
    new Promise((s, j) => j("a"))
      .then(null, (res) => new Promise((s) => s(res + "d")))
      .then((res) => new Promise((s, j) => j(res + "e")))
      .then(null, (reason) => {
        assert(reason === "ade");
        done();
      });
  });
  it("then 链式调用之复杂", (done) => {
    new Promise((s, j) => j("a"))
      .then(null, (res) => res + "b")
      .then((res) => res + "c")
      .then((res) => {
        return new Promise((resolve) => resolve(res + "d"));
      })
      .then((res) => new Promise((s, j) => j(res + "e")))
      .then(null, (reason) => {
        assert(reason === "abcde");
        done();
      });
  });
  it("透传", (done) => {
    new Promise((s, j) => j("aaa"))
      .then((res) => "xxx")
      .then(null, (err) => {
        assert(err === "aaa");
        done();
      });
  });
  it("catch", (done) => {
    new Promise((s, j) => j("a"))
      .then(() => "xxx")
      .catch((err) => err + "b")
      .then((res) => res + "c")
      .then((res) => {
        return new Promise((resolve) => resolve(res + "d"));
      })
      .then((res) => new Promise((s, j) => j(res + "e")))
      .catch((err) => {
        assert(err === "abcde");
        done();
      });
  });
  // it("静态方法 resolve", (done) => {
  //   Promise.resolve(1).then((res) => {
  //     assert(res === 1);
  //     done();
  //   });
  // });
  // it("静态方法 reject", (done) => {
  //   Promise.reject(1).then(null, (reason) => {
  //     assert(reason === 1);
  //     done();
  //   });
  // });
  // it("静态方法 race resolve", (done) => {
  //   const p1 = new Promise((resolve) => resolve(1));
  //   const p2 = new Promise((resolve) => {
  //     setTimeout(() => resolve(2), 10);
  //   });
  //   Promise.race([p1, p2]).then((res) => {
  //     assert(res === 1);
  //     done();
  //   });
  // });
  // it("静态方法 race reject", (done) => {
  //   const p1 = new Promise((resolve, reject) => reject(1));
  //   const p2 = new Promise((resolve) => {
  //     setTimeout(() => resolve(2), 10);
  //   });
  //   Promise.race([p1, p2]).then(null, (res) => {
  //     assert(res === 1);
  //     done();
  //   });
  // });
  // it("静态方法 all resolve", (done) => {
  //   const p1 = new Promise((resolve) => resolve(1));
  //   const p2 = new Promise((resolve) => {
  //     setTimeout(() => resolve(2), 10);
  //   });
  //   Promise.all([p1, p2]).then((res) => {
  //     expect([1, 2]).to.be.eql(res)
  //     done();
  //   });
  // });
  // it("静态方法 all reject", (done) => {
  //   const p1 = new Promise((resolve) => resolve(1));
  //   const p2 = new Promise((s, j) => j(2));
  //   Promise.all([p1, p2]).then(null, (res) => {
  //     assert(res === 2);
  //     done();
  //   });
  // });
});
